/**
 * 
 */
package cx3k.GUI;

import java.awt.*;
import java.util.ArrayList;

import javax.swing.*;

import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Course;

/**
 * @author Chris
 *
 */
public class CompletedClasses extends JPanel {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Course> courses;
	private ClassInfo classInfo;
	private int nextColumn;
	private int nextRow;
	private GridBagLayout gbl;
	private GridBagConstraints gbc;
	private int desiredWidth;
	private Scheduler scheduler;
	
	public CompletedClasses (ClassInfo classInfo, Scheduler s) {
		this.setBackground(MainWindow.MAINCOLOR);
		this.gbl = new GridBagLayout();
		this.gbc = new GridBagConstraints();
		this.setLayout(gbl);
		this.courses = new ArrayList<Course>();
		this.classInfo = classInfo;
		this.nextColumn = this.nextRow = 0;
		this.desiredWidth = 155;
		this.scheduler = s;
		
		gbc.gridx = 0;
		gbc.gridy = 0; 
		gbc.gridwidth = 1; 
		gbc.gridheight = 1; 
		gbc.weightx = 100/3; 
		gbc.weighty = 10; 
		gbc.fill = GridBagConstraints.NONE; 
		gbc.anchor = GridBagConstraints.LINE_START; 
	}
	
	public void addClass(Course course) {
		if (!this.courses.contains(course)) {
			this.courses.add(course);
			this.scheduler.addCompletedClass(course);
			CompletedClassDisplay cd = new CompletedClassDisplay(new ScheduledCourseGroup(course), this.classInfo, this);
			cd.setPreferredSize(new Dimension(desiredWidth, 30));
			this.gbc.gridx = this.nextColumn++;
			this.gbc.gridy = this.nextRow;
			if (nextColumn == 3) {
				nextColumn = 0;
				nextRow++;
			}
			this.gbl.setConstraints(cd, this.gbc);
			this.add(cd);
		}
	}
	
	
	public void update(ArrayList<Course> courses) {
		this.removeAll();
		this.courses = new ArrayList<Course>();
		this.nextColumn = this.nextRow = 0;
		
		for (Course c: courses) {
			this.courses.add(c);
			CompletedClassDisplay cd = new CompletedClassDisplay(new ScheduledCourseGroup(c), this.classInfo, this);
			cd.setPreferredSize(new Dimension(desiredWidth, 30));
			this.gbc.gridx = this.nextColumn++;
			this.gbc.gridy = this.nextRow;
			if (nextColumn == 3) {
				nextColumn = 0;
				nextRow++;
			}
			this.gbl.setConstraints(cd, this.gbc);
			this.add(cd);
		}
		
		this.updateUI();
	}
	
	
	public void removeClass(Course c) {
		this.scheduler.removeCompletedClass(c);
		this.update(scheduler.getCompletedClasses());
	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
